Ext.onReady(function() {

    Ext.form.Field.prototype.msgTarget = 'qtip';
    Ext.QuickTips.init();
    //元数据的store
    var store2 = new Ext.data.Store({
        storeId: 'store2',
        proxy: new Ext.data.HttpProxy({
            url: 'metadata.action?code=refuel'
        }),
        reader: new Ext.data.JsonReader({
            fields: [
                {
                    name: 'name'
                }, {
                    name: 'value'
                }
            ]
        })
    });

    //获取车辆列表
    var carList_store = new Ext.data.Store({
        storeId: 'store3',
        proxy: new Ext.data.HttpProxy({
            url: 'Get_Car_List.action'
        }),
        reader: new Ext.data.JsonReader({
            fields: [
                {
                    name: 'name'
                }, {
                    name: 'value'
                }
            ]
        })
    });

    //获取驾驶员列表
    var driverList_store = new Ext.data.Store({
        storeId: 'store4',
        proxy: new Ext.data.HttpProxy({
            url: 'Get_Data_List.action?method=driver'
        }),
        reader: new Ext.data.JsonReader({
            fields: [
                {
                    name: 'name'
                }, {
                    name: 'value'
                }
            ]
        })
    });

    //获取加油卡列表
    var cardList_store = new Ext.data.Store({
        storeId: 'store4',
        proxy: new Ext.data.HttpProxy({
            url: 'Get_Data_List.action?method=card'
        }),
        reader: new Ext.data.JsonReader({
            fields: [
                {
                    name: 'name'
                }, {
                    name: 'value'
                }
            ]
        })
    });

    var cardOrArea_store = new Ext.data.Store({
        storeId: 'store3',
        proxy: new Ext.data.HttpProxy({
            url: 'Get_AreaOrCard_List.action'
        }),
        reader: new Ext.data.JsonReader({
            fields: [
                {
                    name: 'name'
                }, {
                    name: 'value'
                }
            ]
        })
    });

    var store_refuel = new Ext.data.Store({
        storeId: 'store1',
        proxy: new Ext.data.HttpProxy({
            url: 'metadata.action?code=refuel'
        }),
        reader: new Ext.data.JsonReader({
            fields: [
                {
                    name: 'name'
                }, {
                    name: 'value'
                }
            ]
        })
    });



    var refuel = new Ext.FormPanel({
        id: 'refuel',
        frame: true,
        bodyStyle: 'padding:5px 5px 0',
        width: 600,
        items: [{
                layout: 'column',
                items: [{
                        columnWidth: .5,
                        layout: 'form',
                        items: [{
                                xtype: 'hidden',
                                name: 'id'
                            }, new Ext.form.ComboBox({
                                fieldLabel: '车牌号码',
                                name: 'car_code',
                                displayField: 'name',
                                valueField: 'name',
                                emptyText: '车牌号码',
                                anchor: '95%',
                                typeAhead: true,
                                queryParam: 'name', autoSelect: false,  triggerAction : 'all', store: carList_store
                            }), new Ext.form.ComboBox({
                                fieldLabel: '加油类型',
                                allowBlank: false,
                                hiddenName: 'type',
                                store: store_refuel,
                                displayField: 'name',
                                valueField: 'value',
                                typeAhead: true,
                                triggerAction: 'all',
                                emptyText: '---请选择---',
                                anchor: "95%",
                                name: 'type'
                            }), {
                                xtype: 'numberfield',
                                fieldLabel: '加油金额(元)',
                                emptyText: '加油金额（必填）',
                                allowBlank: false,
                                name: 'money',
                                anchor: '95%'
                            }, {
                                xtype: 'numberfield',
                                fieldLabel: '加油数量(升)',
                                emptyText: '加油数量（必填）',
                                allowBlank: false,
                                name: 'quantity',
                                anchor: '95%'
                            }, {
                                xtype: 'datefield',
                                fieldLabel: '加油时间',
                                emptyText: '加油时间（必填）',
                                allowBlank: false,
                                name: 'ref_date',
                                altFormats: 'Ymd',
                                format: 'Ymd',
                                dateFormat: 'Ymd',
                                anchor: '95%'
                            }]
                    }, {
                        columnWidth: .5,
                        layout: 'form',
                        items: [{
                                columnWidth: .5,
                                layout: 'form',
                                items: [new Ext.form.ComboBox({
                                        fieldLabel: '驾驶员',
                                        name: 'driver',
                                        displayField: 'name',
                                        valueField: 'name',
                                        emptyText: '驾驶员',
                                        anchor: '95%',
                                        typeAhead: true,
                                        triggerAction: 'all',
                                        queryParam: 'name',
                                        minChars: 2,
                                        store: driverList_store
                                    }), new Ext.form.ComboBox({
                                        fieldLabel: '油卡编号',
                                        name: 'card_code',
                                        displayField: 'name',
                                        valueField: 'name',
                                        emptyText: '油卡编号',
                                        anchor: '95%',
                                        typeAhead: true,
                                        triggerAction: 'all',
                                        store: cardList_store
                                    }), {
                                        xtype: 'numberfield',
                                        fieldLabel: '油料单价(元)',
                                        emptyText: '油料单价',
                                        hiddenName: 'ref_price',
                                        anchor: '95%'
                                    }, {
                                        xtype: 'textfield',
                                        fieldLabel: '加油地点',
                                        emptyText: '加油地点',
                                        name: 'ref_area',
                                        anchor: '95%'
                                    }, {
                                        xtype: 'numberfield',
                                        fieldLabel: '路码数',
                                        emptyText: '路码数（必填）',
                                        allowBlank: false,
                                        name: 'yard',
                                        anchor: '95%'
                                    }]
                            }]
                    }]
            }, {
                xtype: 'htmleditor',
                id: 'remark',
                name: 'remark',
                fieldLabel: '备注',
                // height:200,
                anchor: '98%'
            }]
    });

    var win = new Ext.Window({
        id: 'win',
        title: "添加车辆加油记录",
        border: false,
        bodyBorder: false,
        // layout:'fit',
        width: 600,
        closeAction: 'hide',
        modal: true,
        tbar: ['-', {
                text: ' 提交并继续',
                handler: function() {
                    onSubmit();
                }
            }, '-', {
                text: '重置',
                handler: function() {
                    refuel.getForm().reset();
                }
            }],
        items: [refuel]
    }).show();

    function onSubmit() {
        // 提交表单
        if (refuel.getForm().isValid()) {
            var submitValues = refuel.getForm().getValues();
            for (var param in submitValues) {
                if (refuel.form.findField(param)
                        && refuel.form.findField(param).emptyText == submitValues[param]) {
                    refuel.form.findField(param).setValue(' ');
                }
            }
            refuel.getForm().submit({
                url: 'RefuelAction.action',
                waitMsg: '数据保存中...',
                submitEmptyText: false,
                success: function(c, d) {
                    Ext.MessageBox.alert("操作信息", "成功信息保存！");
                    refuel.getForm().reset();
                },
                failure: function(c, d) {
                    Ext.MessageBox.show({
                        title: "操作信息",
                        msg: "信息保存出错，请联系管理员！",
                        buttons: Ext.MessageBox.OK,
                        icon: "ext-mb-error"
                    });
                }
            });
        }
    }
});